import 'package:flutter/widgets.dart';
import 'package:whale/helper/utils/um/page.dart';
import 'package:whale/helper/utils/um/um_man.dart';
import 'package:whale/pages/home/main/widgets/image_generator.dart';

import '../../../helper/utils/util.dart';
import '../../app_tab/index.dart';
import 'index.dart';

class HomeController extends GetxController {
  HomeController();

  final state = HomeState();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    tabRefresh();
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() async {
    super.onReady();
    // UmMan().onPageStart(UmPage.home);
    onRefresh();
  }

  void updateImageData() async {
    state.imageData = await ImageGenerator().generate('我是标题2');
    state.image = await ImageGenerator().generatorImage(size: Size(50, 50));
    update(['paint_image']);
  }

  void tabRefresh() {
    var tc = Get.find<AppTabController>();
    tc.state.isRefreshing.listen((p0) async {
      if (p0 && tc.state.currentIndex.value == 0) {
        await onRefresh();
        tc.state.isRefreshing.value = false;
        tc.updateRefresh();
      }
    });
  }

  Future<void> onRefresh() async {
    Hud.show();
    await Future.delayed(Duration(seconds: 3));
    Hud.dismiss();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    // UmMan().onPageEnd(UmPage.home);
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
